/********************************************************
 Name          : common_def.c
 Author        : Khachatur Gyozalyan
 Copyright     : Copyright by Antel Systems
 Description   :
 **********************************************************/
// Include Files
#include "common_def.h"
#include "at25dfx.h"
#include "file_system.h"
#include "CfgParams.h"
#include "printf-stdarg.h"

/*================================================================================*\
  Author      :       Khachatur Gyozalyan
  ================================================================================
  Function    :       Reset_MCU
  Description :       Reset MCU using WDT reset.
  Return      :       void
  Parameters  :       void
  Note        :
\*================================================================================*/
void Reset_MCU(void)
{
	  Disable_global_interrupt();
	  wdt_enable(300000);
	  while (1);
}

/*================================================================================*\
  Author      :       Khachatur Gyozalyan
  ================================================================================
  Function    :       Set_boot_option
  Description :       Write Boot option to the user page memorry.
  Return      :       void
  Parameters  :       int 0 - Noraml boot
                          1 - Boot Default
  Note        :
\*================================================================================*/
void Set_boot_option(int option)
{
	unsigned short data;
	switch (option)
	{
	case BOOT_OPTION_USER:
		data = (unsigned short)USER_BOOT_VALUE;
		break;
	case BOOT_OPTION_DEFAULT:
		data = (unsigned short)DEFAULT_BOOT_VALUE;
		break;
	case BOOT_OPTION_PERM_DEFAULT:
		data = (unsigned short)PERM_DEFAULT_BOOT_VALUE;
		break;
	}
	flashc_memcpy((void *)AVR32_FLASHC_USER_PAGE_ADDRESS,&data, 2,1);
}

/*================================================================================*\
  Author      :       Khachatur Gyozalyan
  ================================================================================
  Function    :       Get_boot_option
  Description :       Write Boot option to the user page memorry.
  Return      :       int 0 - Noraml boot
                          1 - Boot Default
  Parameters  :       void
  Note        :
\*================================================================================*/
int Get_boot_option(void)
{
	unsigned short *data;
	data = (unsigned short *)AVR32_FLASHC_USER_PAGE_ADDRESS;
	switch (*data)
	{
	case USER_BOOT_VALUE:
		return BOOT_OPTION_USER;
		break;
	case DEFAULT_BOOT_VALUE:
		return BOOT_OPTION_DEFAULT;
		break;
	case PERM_DEFAULT_BOOT_VALUE:
		return BOOT_OPTION_PERM_DEFAULT;
		break;
	}
	return BOOT_OPTION_USER;
}

/*================================================================================*\
  Author      :       Khachatur Gyozalyan
  ================================================================================
  Function    :       print_version_info
  Description :       This function prints version info (Defined in common_def.h).
  Return      :       void
  Parameters  :       void
  Note        :
\*================================================================================*/
void initial_version_struct(void)
{
	unsigned char *local_ptrOnVersionData = malloc(SIZEOF_IDENTIFICATION);
	if(FACTORY_CONFIG_IS_AVALIABLE)
	{
		struct file_t file;
		find_file(&file, "FCFG");

		int FCFG_address = file.sector_number * AT25DFX_SECTOR_SIZE + SIZEOF_FILEHEADER;
		at25dfx_read_bytes(FCFG_address,SIZEOF_IDENTIFICATION,local_ptrOnVersionData);

		memcpy(ANTEL_VERSION.HW_SW.ProductModel,local_ptrOnVersionData,SIZEOF_MODEL);
		local_ptrOnVersionData += SIZEOF_MODEL;
		ANTEL_VERSION.HW_SW.ProductModel[SIZEOF_MODEL] = '\0';
		memcpy(ANTEL_VERSION.HW_SW.Description,local_ptrOnVersionData,SIZEOF_DESCRIPTION);
		local_ptrOnVersionData += SIZEOF_DESCRIPTION;
		ANTEL_VERSION.HW_SW.Description[SIZEOF_DESCRIPTION] = '\0';
		memcpy(ANTEL_VERSION.HW_SW.PCBA_Rev_Ver,local_ptrOnVersionData,SIZEOF_PCBA_VERSION);
		local_ptrOnVersionData += SIZEOF_PCBA_VERSION;
		ANTEL_VERSION.HW_SW.PCBA_Rev_Ver[SIZEOF_PCBA_VERSION] = '\0';
		memcpy(ANTEL_VERSION.HW_SW.EmbSoftImageVer,local_ptrOnVersionData,SIZEOF_SOFTWARE_VERSION);
		local_ptrOnVersionData += SIZEOF_SOFTWARE_VERSION;
		ANTEL_VERSION.HW_SW.EmbSoftImageVer[SIZEOF_SOFTWARE_VERSION] = '\0';
		memcpy(ANTEL_VERSION.HW_SW.GMSKImageVer,local_ptrOnVersionData,SIZEOF_GMSK_IMAGE_VERSION);
		local_ptrOnVersionData += SIZEOF_GMSK_IMAGE_VERSION;
		ANTEL_VERSION.HW_SW.GMSKImageVer[SIZEOF_GMSK_IMAGE_VERSION] = '\0';
		memcpy(ANTEL_VERSION.HW_SW.FSKImageVer,local_ptrOnVersionData,SIZEOF_FSK_IMAGE_VERSION);
		local_ptrOnVersionData += SIZEOF_FSK_IMAGE_VERSION;
		ANTEL_VERSION.HW_SW.FSKImageVer[SIZEOF_FSK_IMAGE_VERSION] = '\0';
		memcpy(ANTEL_VERSION.HW_SW.QAMImageVer,local_ptrOnVersionData,SIZEOF_QAM_IMAGE_VERSION);
		local_ptrOnVersionData += SIZEOF_QAM_IMAGE_VERSION;
		ANTEL_VERSION.HW_SW.QAMImageVer[SIZEOF_QAM_IMAGE_VERSION] = '\0';
		memcpy(ANTEL_VERSION.HW_SW.ZigBeeImageVer,local_ptrOnVersionData,SIZEOF_ZIGBEE_IMAGE_VERSION);
		local_ptrOnVersionData += SIZEOF_ZIGBEE_IMAGE_VERSION;
		ANTEL_VERSION.HW_SW.ZigBeeImageVer[SIZEOF_ZIGBEE_IMAGE_VERSION] = '\0';
		local_ptrOnVersionData += SIZEOF_IDENT_RESERVED;
		memcpy(ANTEL_VERSION.SysID.SN_MAC,local_ptrOnVersionData,SIZEOF_SERIAL_NUMBER_MAC);
		local_ptrOnVersionData += SIZEOF_SERIAL_NUMBER_MAC;
		ANTEL_VERSION.SysID.SN_MAC[SIZEOF_SERIAL_NUMBER_MAC] = '\0';
		memcpy(ANTEL_VERSION.SysID.Manufactor_SN,local_ptrOnVersionData,SIZEOF_MANUFACTORING_SERIAL_NUMBER);
		ANTEL_VERSION.SysID.Manufactor_SN[SIZEOF_MANUFACTORING_SERIAL_NUMBER] = '\0';
		local_ptrOnVersionData -= SIZEOF_IDENTIFICATION - SIZEOF_SECRET_KEY - SIZEOF_MANUFACTORING_SERIAL_NUMBER;
		free(local_ptrOnVersionData);
	}
}
void print_version_info(void)
{
	printk("\r\n%s.\r\n", ANTEL_VERSION.HW_SW.Description);
	printk("%s%s\r\n", "Product ID: ", ANTEL_VERSION.HW_SW.ProductModel);
	printk("%s%s\r\n", "Product S/N: ", ANTEL_VERSION.SysID.SN_MAC);
	printk("%s%s\r\n", "MFG S/N: ", ANTEL_VERSION.SysID.Manufactor_SN);
	printk("%s%s\r\n", "Hardware: ", ANTEL_VERSION.HW_SW.PCBA_Rev_Ver);
	printk("%s%s\r\n", "Runtime Software: ", ANTEL_VERSION.HW_SW.EmbSoftImageVer);
	printk("%s%s\r\n", "GMSK Image: ", ANTEL_VERSION.HW_SW.GMSKImageVer);
	printk("%s%s\r\n", "MFSK Image: ", ANTEL_VERSION.HW_SW.FSKImageVer);
	printk("%s%s\r\n", "MQAM Image: ", ANTEL_VERSION.HW_SW.QAMImageVer);
}

